import os
import sys

from collections import defaultdict

os.environ['DJANGO_SETTINGS_MODULE'] = 'psd.settings'

import sys
from psd.register.models import Person, RegRecord, Event

def sort_by_field(lines, idx):
    d = defaultdict(list)
    for k, v in lines:
        d[v[idx]].append((k, v))
    return d

def explain(psdid):
    regs = RegRecord.objects.filter(event='anthony1')
    ev = Event.objects.get(event='anthony1')
    me = RegRecord.objects.get(event='anthony1', psdid=psdid)
    liked = []

    answers = defaultdict(list)
    for x in regs:
        if x.psdid == psdid:
            continue
        answer, reason = me.explain_will_date(x, ev)
        answers[answer].append((x.psdid, reason))
        if answer:
            liked.append(x)
    print "%s approved of %s people:" % (me.nickname, len(answers[True]))
    print "  " + ', '.join(x[0] for x in answers[True])
    print
    print "Here's why %s rejected people..." % me.nickname
    rejects = answers[False]
    explain_rejections(rejects)

    answers = defaultdict(list)
    for x in regs:
        if x.psdid == psdid:
            continue
        answer, reason = x.explain_will_date(me, ev)
        answers[answer].append((x.psdid, reason))
    print "%s was approved of by %s people overall:" % (me.nickname, len(answers[True]))
    print "  " + ', '.join(x[0] for x in answers[True])
    print
    print "Here's why people rejected %s..." % me.nickname
    rejects = answers[False]
    explain_rejections(rejects)

    answers = defaultdict(list)
    for x in liked:
        answer, reason = x.explain_will_date(me, ev)
        answers[answer].append((x.psdid, reason))
    print "%s was approved of by %s people they liked:" % (me.nickname, len(answers[True]))
    print "  " + ', '.join(x[0] for x in answers[True])
    print
    print "Here's why people %s liked rejected %s..." % (me.nickname, me.nickname)
    rejects = answers[False]
    explain_rejections(rejects)



def explain_rejections(rejects):
    d = sort_by_field(rejects, 0)
    if d['gender']:
        print "%s rejections because of gender:" % len(d['gender'])
        d2 = sort_by_field(d['gender'], 1)
        print "  %s with no overlap:" % len(d2['no overlap']),
        print ", ".join("%s (%s)" % (k[0], k[1][2]) for k in d2['no overlap'])
        print "  %s with some overlap:" % len(d2['overlap']),
        print ", ".join("%s (%s)" % (k[0], k[1][2]) for k in d2['overlap'])
        print

    if d['age']:
        print "%s rejections because of age:" % len(d['age'])
        d2 = sort_by_field(d['age'], 1)
        print "  %s too young:" % len(d2['too young'])
        d3 = sort_by_field(d2['too young'], 2)
        print "    %s slightly:" % len(d3['slightly']),
        print ", ".join("%s (%s)" % (k[0], k[1][3]) for k in d3['slightly'])
        print "    %s fully:" % len(d3['fully']),
        print ", ".join("%s (%s)" % (k[0], k[1][3]) for k in d3['fully'])

        print "  %s too old:" % len(d2['too old'])
        d3 = sort_by_field(d2['too old'], 2)
        print "    %s slightly:" % len(d3['slightly']),
        print ", ".join("%s (%s)" % (k[0], k[1][3]) for k in d3['slightly'])
        print "    %s fully:" % len(d3['fully']),
        print ", ".join("%s (%s)" % (k[0], k[1][3]) for k in d3['fully'])
        print

    if d['kink']:
        print "%s rejections because of kink:" % len(d['kink'])
        print ("%s:" % (d['kink'][0][1][1])),
        print ", ".join(k[0] for k in d['kink'])
        print

    if d['monog']:
        print "%s rejections because of monogamy:" % len(d['monog']),
        print ", ".join(k[0] for k in d['monog'])
        print


    return 

if __name__ == '__main__':
    explain(sys.argv[1])
